package oa.base.util.json;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.CycleDetectionStrategy;

/**
 * json 转换工具
 * @author lilang
 *
 */
public class JsonUtils {
	
	
	
	
	
	private Map<String, JsonValueProcessor> conversion = new HashMap<String, JsonValueProcessor>();
	protected String[] defaultExcludes = new String[]{"offset","createdDtFormatted","updatedDtFormatted"};

	public void setConversion(Map<String, JsonValueProcessor> conversion) {
		this.conversion = conversion;
	}
	public JsonUtils(){
		super();
		initProcessor();
	}
	public void initProcessor(){
		conversion.put("java.util.Date", new JsonDateValueProcessor());
	}
	/**
	 * 返回json对象
	 * 
	 * @param obj
	 * @param excludes
	 * @return
	 * @throws Exception
	 */
	public String getExtJson(Object obj, String[] excludes) throws Exception {
		String strRet = "";
		try {
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
			Set<String> keySet = this.conversion.keySet();
			for (String key : keySet) {
				Class<?> clazz= Class.forName(key);
				jsonConfig.registerJsonValueProcessor(clazz, this.conversion.get(key));
			}
			
			if (null != excludes) {
				jsonConfig.setExcludes(excludes);
			}
			strRet = JSONObject.fromObject(obj, jsonConfig).toString();
		} catch (Throwable e) {
			throw new Exception(e);
		}
		return strRet;
	}
	
	/**
	 * 返回json对象
	 * 
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public String getExtJson(String obj) throws Exception {
		String strRet = "\"" + obj + "\"";
		return strRet;
	}

	/**
	 * 返回json对象
	 * 
	 * @param obj
	 * @param excludes
	 * @return
	 * @throws Exception
	 */
	public String getSpeExtJson(Object obj, String[] excludes) throws Exception {
		String strRet = "";
		try {
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
			Set<String> keySet = this.conversion.keySet();
			for (String key : keySet) {
				Class<?> clazz= Class.forName(key);
				jsonConfig.registerJsonValueProcessor(clazz, this.conversion.get(key));
			}
			if (null != excludes) {
				jsonConfig.setExcludes(excludes);
			}

			strRet = JSONObject.fromObject(obj, jsonConfig).toString();
		} catch (Throwable e) {
			throw new Exception(e);
		}
		return strRet;
	}

	public String getExtArrayJson(Object obj) throws Exception{
		return this.getExtArrayJson(obj, defaultExcludes);
	}
	/**
	 * 返回json对象
	 * 
	 * @param obj
	 *            需要转换的数组对象
	 * @param excludes
	 *            不需要返回的json属性
	 * @return
	 * @throws Exception
	 */
	public String getExtArrayJson(Object obj, String[] excludes)
			throws Exception {
		String strRet = "";
		try {
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
			Set<String> keySet = this.conversion.keySet();
			for (String key : keySet) {
				Class<?> clazz= Class.forName(key);
				jsonConfig.registerJsonValueProcessor(clazz, this.conversion.get(key));
			}
			if (null != excludes) {
				jsonConfig.setExcludes(excludes);
			}
			strRet = JSONArray.fromObject(obj, jsonConfig).toString();

		} catch (Throwable e) {
			throw new Exception(e);
		}
		return strRet;
	}

}
